﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using IP.Core.IPCommon;
using IP.Core.IPException;
using IP.Core.IPData;
using IP.Core.IPUserService;

using BtdUS;
using BtdDS;
using BtdDS.CDBNames;

namespace BtdApp.DanhMuc
{
    public partial class f601_dm_loai_hang_muc_de : Form
    {
        public f601_dm_loai_hang_muc_de()
        {
            InitializeComponent();
            format_controls();
        }

        #region Members
        US_DM_LOAI_HANG_MUC m_us_dm_loai_hm = new US_DM_LOAI_HANG_MUC();
        DS_DM_LOAI_HANG_MUC m_ds_dm_loai_hm = new DS_DM_LOAI_HANG_MUC();
        US_DM_LOAI_HANG_MUC m_us_dm_loai_hm_cha = new US_DM_LOAI_HANG_MUC();
        e_form_mode m_e_form_mode = e_form_mode.THEM_DU_LIEU;
        #endregion

        #region Data Structure
        private enum e_form_mode
        {
            THEM_DU_LIEU = 1
            , SUA_DU_LIEU = 2
        }
        #endregion

        #region Public Interfaces
        public void display(US_DM_LOAI_HANG_MUC ip_us)
        {
            // hiển thị để thêm
            m_e_form_mode = e_form_mode.THEM_DU_LIEU;
            m_us_dm_loai_hm_cha = ip_us;
            this.ShowDialog();
        }
        public void displayIsEdit(US_DM_LOAI_HANG_MUC ip_us)
        {
            // hiển thị để sửa
            m_e_form_mode = e_form_mode.SUA_DU_LIEU;
            m_us_dm_loai_hm = ip_us;
            this.ShowDialog();
        }
        #endregion

        #region Private Methods
        private void format_controls()
        {
            CControlFormat.setFormStyle(this);

            set_define_events();

            m_lbl_title.Font = new Font("Arial", 16);
            m_lbl_title.ForeColor = Color.DarkRed;
            m_lbl_title.TextAlign = ContentAlignment.MiddleCenter;
        }
        private void set_initial_form_load()
        {
            switch (m_e_form_mode)
            {
                case e_form_mode.THEM_DU_LIEU:
                    us_loai_cha_2_form();
                    break;
                case e_form_mode.SUA_DU_LIEU:
                    us_object_2_form(m_us_dm_loai_hm);
                    break;
            }
        }
        private void us_object_2_form(US_DM_LOAI_HANG_MUC ip_us_dm)
        {
            m_us_dm_loai_hm_cha = new US_DM_LOAI_HANG_MUC(ip_us_dm.dcID_LOAI_CHA);
            m_txt_ma_loai.Text = ip_us_dm.strMA_LOAI;
            m_txt_ten_loai.Text = ip_us_dm.strTEN_LOAI;
            m_txt_stt.Text = CIPConvert.ToStr(ip_us_dm.dcSO_THU_TU, "####");
            m_txt_loai_cha.Text = m_us_dm_loai_hm_cha.strTEN_LOAI;
            if (m_us_dm_loai_hm.strLA_LA_YN.Equals("Y"))
                m_rb_Y.Checked = true;
            else
                m_rb_N.Checked = true;

        }

        private void form_2_us_object(US_DM_LOAI_HANG_MUC op_us_dm)
        {
            op_us_dm.dcID = op_us_dm.dcID;
            op_us_dm.strTEN_LOAI = m_txt_ten_loai.Text;
            op_us_dm.strMA_LOAI = m_txt_ma_loai.Text;
            op_us_dm.strLA_LA_YN = m_rb_Y.Checked ? "Y" : "N";
            op_us_dm.dcSO_THU_TU = CIPConvert.ToDecimal( m_txt_stt.Text);
            op_us_dm.dcID_LOAI_CHA = m_us_dm_loai_hm_cha.dcID;
            op_us_dm.dcLEVEL_TREN_CAY = m_us_dm_loai_hm_cha.dcLEVEL_TREN_CAY + 1;
        }
        //Chọn loại hạng mục từ form danh mục loại hạng mục
        private void chon_loai_hm_cha()
        {
            f600_dm_loai_hang_muc m_frm600 = new f600_dm_loai_hang_muc();
            m_us_dm_loai_hm_cha = m_frm600.display_chon_loai_hm_cha();
            if (!m_us_dm_loai_hm_cha.IsIDNull())
                us_loai_cha_2_form();
        }
        //Hàm dùng để gán tên loại hamjg mục lên form
        private void us_loai_cha_2_form()
        {
            if (m_us_dm_loai_hm_cha.dcID != -1)
            {
                m_txt_loai_cha.Text = m_us_dm_loai_hm_cha.strTEN_LOAI;
                m_txt_stt.Text = get_stt_by_stt_cha(m_us_dm_loai_hm_cha.dcSO_THU_TU);
                m_txt_ma_loai.Text = get_ma_by_ma_cha(m_us_dm_loai_hm_cha.strMA_LOAI);
            }
        }
        private string get_stt_by_stt_cha(decimal ip_dc_stt)
        {           
            string v_str_stt = CIPConvert.ToStr(ip_dc_stt, "####");
            v_str_stt = v_str_stt.Substring(v_str_stt.IndexOf('0') + 1);
            v_str_stt = "1" + v_str_stt;
            return CIPConvert.ToStr (ip_dc_stt + CIPConvert.ToDecimal(v_str_stt), "####");
        }
        private string get_ma_by_ma_cha(string ip_str_ma)
        {
            string v_str_ma = ip_str_ma.Substring(0, ip_str_ma.IndexOf('.')); 
            string v_str_stt = ip_str_ma.Substring(ip_str_ma.IndexOf('.')+1);
            v_str_ma = v_str_ma + "." + get_stt_by_stt_cha(CIPConvert.ToDecimal(v_str_stt));
            return v_str_ma;
        }
        private void save_loai_hang_muc()
        {
            if (check_gia_tri_us_loai_hang_muc())
            {
                try
                {
                    m_us_dm_loai_hm.BeginTransaction();
                    form_2_us_object(m_us_dm_loai_hm);
                    switch (m_e_form_mode)
                    {
                        case e_form_mode.THEM_DU_LIEU:
                            m_us_dm_loai_hm.Insert();
                            break;
                        case e_form_mode.SUA_DU_LIEU:
                            m_us_dm_loai_hm.Update();
                            break;
                        default:
                            break;
                    }
                    m_us_dm_loai_hm.CommitTransaction();
                }
                catch (Exception v_e)
                {
                    m_us_dm_loai_hm.Rollback();                
                throw v_e;
                }
                

            }
        }

        private bool check_gia_tri_us_loai_hang_muc()
        {
            return true;
        }
        #endregion

        #region Events
        private void set_define_events()
        {
            m_cmd_chon_loai_hm.Click += new EventHandler(m_cmd_chon_loai_hm_Click);
            m_cmd_exit.Click += new EventHandler(m_cmd_exit_Click);
            m_cmd_luu.Click += new EventHandler(m_cmd_luu_Click);
            this.Load += new EventHandler(f601_dm_loai_hang_muc_de_Load);
            this.KeyDown += new KeyEventHandler(f601_dm_loai_hang_muc_de_KeyDown);
        }

        void m_cmd_chon_loai_hm_Click(object sender, EventArgs e)
        {
            try
            {
                chon_loai_hm_cha();
            }
            catch (Exception v_e)
            {
                CSystemLog_301.ExceptionHandle(v_e);
            }
        }

        void f601_dm_loai_hang_muc_de_Load(object sender, EventArgs e)
        {
            try
            {
                set_initial_form_load();
            }
            catch (Exception v_e)
            {
                CSystemLog_301.ExceptionHandle(v_e);
            }
        }

        void f601_dm_loai_hang_muc_de_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                switch (e.KeyCode)
                {
                    case Keys.Escape:
                        this.Close();
                        break;
                }
            }
            catch (Exception v_e)
            {
                CSystemLog_301.ExceptionHandle(v_e);
            }
        }       

        void m_cmd_luu_Click(object sender, EventArgs e)
        {
            try
            {
                save_loai_hang_muc();
                this.Close();
            }
            catch (Exception v_e)
            {
                CSystemLog_301.ExceptionHandle(v_e);
            }
        }

        void m_cmd_exit_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception v_e)
            {
                CSystemLog_301.ExceptionHandle(v_e);
            }
        }
        #endregion

    }
}
